<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品生产信息
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 * @version 1.0
 */

quanli_require('module/base/goods/goodsProduce.class.php');

class ajax_goodsProduce {
	
	public function execute() {
		
		if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] >0) {
			
			switch ($_REQUEST['operFlg']) {
				
				case 1:	//商品生产信息列表
					$this->goodsProduceList();
					break;
				case 2:	//获取修改时的商品生产信息
					$this->getgoodsProduce();
					break;
				case 3:	//获取添加时的最大序号
					$this->maxSort();
					break;
				case 4:	//添加商品生产信息
					$this->goodsProduceSave();
					break;
				case 5:	//删除
					$this->goodsProduceDel();
					break;
				case 6:	//使用标志的修改
					$this->changeUse();
					break;
				case 7: //批量添加生产信息
					$this->searchProduct();
					break;		
				default:
					backendError();
			}
			
		} else {
			backendError();
		}
			
	}
	
	/**
	 * 商品生产信息列表
	 */
	private function goodsProduceList() {
		global $G_PGLOB;
		
		$goodsProduce = new goodsProduce($G_PGLOB->DBCA);
		$goosProduceInfo = $goodsProduce->goodsProduceGroup($_REQUEST);
		
		$result_array = array(
				'page'  => $_REQUEST['page'] ,
				'total' => $_REQUEST['count'] ,
				'rows'  => array()
		);
		
		if ($goosProduceInfo) {
            $edit_permission = $G_PGLOB->has_permission(539);
			foreach($goosProduceInfo as &$s_l) {
		
				$goods_produce = ' ';
				
				if ($s_l['goods_produce']) {
					foreach($s_l['goods_produce'] as $k=>&$v) {
						$goods_produce .= $v['pName'] . '　';
					}
				}
				
				if ($edit_permission) {
                    $result_array['rows'][] = array(
                        'id'   => $s_l['pGroupID'],
                        'cell' => array(
                            '<a href="javascript: void(0);" onclick="goodsProduce.edit(' . $s_l['pGroupID'] . ')">维护值</a>' ,
                            $s_l['sortOrder'],
                            $s_l['pGroupName'],
                            $goods_produce,
                            $s_l['useFlg'] == 1 ?
                                '<a href="javascript: void(0);" onclick="goodsProduce.doAction(' . $s_l['pGroupID'] . ', 0) " >启用</a>' :
                                '<a href="javascript: void(0);" onclick="goodsProduce.doAction(' . $s_l['pGroupID'] . ', 1 ) " >禁用</a>'
                        )

                    );
                } else {
                    $result_array['rows'][] = array(
                        'id'   => $s_l['pGroupID'],
                        'cell' => array(
                            $s_l['pGroupID'],
                            $s_l['sortOrder'],
                            $s_l['pGroupName'],
                            $goods_produce,
                            $s_l['useFlg'] == 1 ?
                                '启用' :
                                '禁用'
                        )

                    );
                }
			}
		}
		
		echo json_encode( $result_array );
	}
	
	/**
	 * maxSort
	 */
	private function maxSort() {
		
		global $G_PGLOB;
		
		$goodsProduce = new goodsProduce($G_PGLOB->DBCA);
		$maxSort = $goodsProduce->getmaxSort() + 1;
		
		echo json_encode($maxSort);
		
	}
	
	/**
	 * 添加商品生产信息
	 */
	private function goodsProduceSave() {
		global $G_PGLOB;
		
		$goodsProduce = new goodsProduce($G_PGLOB->DBCA);
		$retArr = $goodsProduce->savegoodsProduce($_REQUEST);
		
		if ($retArr['code'] != 200) {
			echo json_encode(array( 'code' => $retArr['code'] , 'message' => $retArr['message'] )); 
		} else {
			if ($retArr['data'] == 1) {
				echo json_encode(array( 'code' => 200 , 'message' => '商品生产信息修改成功' ));
			} else {
				echo json_encode(array( 'code' => 200 , 'message' => '商品生产信息添加成功' ));
			}
		}
		
	}
	
	/**
	 * 获取修改时的商品生产信息
	 */
	private function getgoodsProduce() {
		
		global $G_PGLOB;
		
		$goodsProduce = new goodsProduce($G_PGLOB->DBCA);
		$goodsProduceInfo = $goodsProduce->goodsProduceGroup($_REQUEST);
		
		echo json_encode($goodsProduceInfo[0]);
		
	}
	
	/**
	 * 删除
	 */
	private function goodsProduceDel() {
		
		global $G_PGLOB;
		
		$pGroupID = $_REQUEST['p_array'][0];
		
		$goodsProduce = new goodsProduce($G_PGLOB->DBCA);
		$retArr = $goodsProduce->delgoodsProduce($pGroupID);
		
		echo json_encode(array( 'code' => $retArr['code'] , 'message' => $retArr['message'] ));
		
	}
	
	/**
	 * 使用标志的修改
	 */
	private function changeUse() {
		global $G_PGLOB;
		
		$pGroupID = $_REQUEST['pGroupID'];
		$useFlg = $_REQUEST['useFlg'];
				
		$goodsProduce = new goodsProduce($G_PGLOB->DBCA);
		$retArr = $goodsProduce->changegoodsProduce($pGroupID, $useFlg);
		
		echo json_encode(array( 'code' => $retArr['code'] , 'message' => $retArr['message'] ));
	}
	
	/**
	 * 批量添加生产信息
	 *
	 */
	public function searchProduct(){
		global $G_SHOP;
		$productHandler = new goodsProduce( $G_SHOP->DBCA ) ;
		$_REQUEST['creator'] = $G_SHOP->currentUserID;
    	$result_array = $productHandler->searchProduct( $_REQUEST ) ;
		
    	if ($result_array['code'] == 200) {
	    	$G_SHOP->smarty->assign_by_ref('product_result', $result_array['data']);
	    	$productTr = $G_SHOP->smarty->fetch('goods/productfetch.tpl');
	    	echo json_encode(array('code' => 200, 'data' => $productTr));
    	} else {
    		echo json_encode($result_array);
    	}
	}
}